//
//  JYTCityZoneView.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/26.
//  Copyright © 2016年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTCityZoneView.h"
#import "JYTUserManager.h"
#import "JYTCity.h"
#import "JYTZone.h"
#import "JYTZoneCollectionViewCell.h"
#import "JYTCollectionHeader.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kMainWindow  [UIApplication sharedApplication].keyWindow

@interface JYTCityZoneView()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UIScrollView *scroll;
@property (nonatomic, strong)NSArray *citys;
@property (nonatomic, strong)JYTCity *currentCity;
@property (nonatomic, strong)UICollectionViewFlowLayout *customLayout;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, copy)NSArray * zones;
@property (nonatomic, strong)UIView * bgView;

@end

@implementation JYTCityZoneView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    
    if (self) {
        self.alpha = 0;
        self.layer.shadowOpacity = 0.5;
        self.layer.shadowOffset = CGSizeMake(0, 0);
        self.layer.shadowRadius = 2.0;
        
        _bgView = [[UIView alloc] initWithFrame: [UIScreen mainScreen].bounds];
        _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        _bgView.alpha = 0;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(dismiss)];
        [_bgView addGestureRecognizer: tap];
        
        
//        _currentCity =[[[JYTUserManager shared] getCityArr] firstObject];
//        _zones = _currentCity.zones;
//        
//        
        BOOL isUsedOffLine = [[JYTUserManager shared] isUsedOffLine];
        
        if (!isUsedOffLine) {
          
        User *user = [[JYTUserManager shared] getUser];
         _currentCity = [[JYTUserManager shared] getCityById:user.cityId];
        _zones = _currentCity.zones;
            
        }else {
            _currentCity =[[[JYTUserManager shared] getCityArr] firstObject];
            _zones = _currentCity.zones;
        }
        
        
        [self setupSubView];
    }
    
    
    return self;
}


- (void)setupSubView
{
    
    [self loadCollectionView];
    
    UILabel *cityLabel = [[UILabel alloc]init];
    [cityLabel setText:@"开通地市"];
    cityLabel.frame = CGRectMake(10, 160, [UIScreen mainScreen].bounds.size.width - 2 *10, 40);
    [cityLabel setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:cityLabel];

    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 200, [UIScreen mainScreen].bounds.size.width - 2 *10, 100)];
    scroll.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    //scroll.delegate = self;
    scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 *10, 100 * 2);
    scroll.bounces = NO;
    scroll.scrollEnabled = YES;
    self.scroll = scroll;
    [self addSubview:scroll];
    
   _citys =  [[JYTUserManager shared] getCityArr];
    
//    for (JYTCity *city in _citys) {
//        MyLog(@"===---%@", city.zone_name);
//    }
    int maxClos = 4;
    
    CGFloat scrollW =[UIScreen mainScreen].bounds.size.width - 2 * 10;
    //宽度和高度
    CGFloat buttonW = scrollW / maxClos;
    CGFloat buttonH = 35;
    
    for (int i = 0; i < _citys.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        JYTCity *city = _citys[i];
        btn.tag = 1000 + i;
        [btn.layer setBorderWidth:1.0];
        [btn setTitle:city.zone_name forState:UIControlStateNormal];
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:5.0];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        
        int col = i % maxClos;
        int row = i / maxClos;
        
        btn.frame = CGRectMake(col * buttonW + 2, row * buttonH + 2, buttonW - 2, buttonH - 2);
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.enabled = NO;
        [scroll addSubview:btn];
        
    }

}

- (void)loadCollectionView
{
     _customLayout=[[UICollectionViewFlowLayout alloc] init];
    _customLayout.minimumInteritemSpacing=5.0f;//左右间隔
    _customLayout.minimumLineSpacing=5.0f;

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 70, [UIScreen mainScreen].bounds.size.width - 2 *10, 90) collectionViewLayout:_customLayout];
    _collectionView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    UINib *nib=[UINib nibWithNibName:@"JYTZoneCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"zone"];
    
    UINib *header=[UINib nibWithNibName:@"JYTCollectionHeader" bundle:nil];
    [self.collectionView registerNib:header forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"city"];
    
    [self addSubview:_collectionView];
    

//    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];
//    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
//    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return  1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.zones.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JYTZoneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zone" forIndexPath:indexPath];
    
   JYTZone *z = self.zones[indexPath.item];
    cell.zoneNameLabel.text = z.zone_name;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(60, 21);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    NSString *reuseIdentifier;
    
    if ([kind isEqualToString: UICollectionElementKindSectionFooter]){
        reuseIdentifier = @"footer";
    }else{
        reuseIdentifier = @"city";
    }
    
    UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind:kind  withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
    
    UILabel *label = (UILabel *)[view viewWithTag:100];
    label.font=[UIFont systemFontOfSize:17];
    label.textColor=[UIColor blackColor];
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
        
        NSString *cName = _currentCity.zone_name;
        //label.text=cName;
        [label setText:cName];
    }
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
        view.backgroundColor = [UIColor lightGrayColor];
        label.text = [NSString stringWithFormat:@"这是footer:%ld",(long)indexPath.section];
    }
    return view;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={CURRENT_SCREEN_WIDTH,44};
    return size;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
   JYTZone *selectZone = _zones[indexPath.row];

    if (self.select) {
        self.select(indexPath.row, selectZone);
    }
    
    [self dismiss];
    
}

- (void)buttonClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 1000;
    self.currentCity = _citys[index];
    MyLog(@"===---%@", self.currentCity.zone_name);
    _zones = self.currentCity.zones;
     [self.collectionView reloadData];
}
- (void)show {
    
    [kMainWindow addSubview: _bgView];
    [kMainWindow addSubview: self];
 
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1;
        _bgView.alpha = 1;
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
        _bgView.alpha = 0;
    } completion:^(BOOL finished) {
 
        [self removeFromSuperview];
        [_bgView removeFromSuperview];
    }];
}
@end
